library(readr); library(dplyr); library(stringr); library(readxl)
library(lfe);library(stargazer);library(ggplot2); library(cowplot)
library(fixest)


# set directory
this_dir <- dirname(rstudioapi::getActiveDocumentContext()$path)

# load analysis data
rev_df <- read_csv(here(this_dir, "analysis_data.csv"))

# load anonymized revolver data -- used in descriptives
anon_df <- read_csv(here(this_dir, "anonymized_revolver_data.csv"))

#-------------------------------------------------------------
# descriptive statistics


used <- as.data.frame(dplyr::select(rev_df, meet, n_meet, treat1, EU_inst))

com <- subset(used, EU_inst == 1)

stargazer(com[,-c(4)],
          covariate.labels = c("Meeting?", "# Meetings", "Months with Revolver Employed"),
          type = "text", title = "Descriptive Statistics")

other <- subset(used, EU_inst != 1)

stargazer(other[,-c(4)],
          covariate.labels = c("Meeting?", "# Meetings", "Months with Revolver Employed"),
          type = "text", title = "Descriptive Statistics")

# visuals
used <- as.data.frame(dplyr::select(rev_df, career_org, meet, n_meet, treat1, EU_inst, subsection))

com <- subset(used, EU_inst == 1)


help <- subset(rev_df, EU_inst == 1)

all_meet <- com %>%
  group_by(career_org) %>%
  summarise(all_meet = sum(n_meet, na.rm=T)) %>%
  arrange(desc(all_meet))


pan_a <- ggplot(all_meet, aes(x = all_meet)) +
  geom_density(fill = "grey", colour = "white") +
  theme_classic() +
  labs(x = "Total Number of Meetings\nBetween 2015 and 2018",
       y = "Density",
       title = "A: Distribution of Total Number of Meetings")

pan_a

pan_b <- ggplot(all_meet[1:15,], aes(y = all_meet, x = reorder(career_org, all_meet))) +
  geom_bar(stat = "identity")+
  coord_flip() +
  theme_classic() +
  labs(x = NULL,
       y = "Total Number of Meetings\nBetween 2015 and 2018",
       title = "B: Top 16 Groups")

pan_b


group_meet <- help %>%
  group_by(subsection) %>%
  summarise(all_meet = sum(n_meet, na.rm=T)) %>%
  arrange(desc(all_meet))

group_meet$subsection <- ifelse(group_meet$subsection == "Other public or mixed entities, created by law whose purpose is to act in the public interest", "Public sector organizations",
                                ifelse(group_meet$subsection == "Non-governmental organisations, platforms and networks and similar", "Non-governmental organizations", 
                                       ifelse(group_meet$subsection == "Professional consultancies", "Lobbying firms and consultancies", 
                                              ifelse(group_meet$subsection == "Companies & groups", "Companies", group_meet$subsection))))



group_meet$subsection <- ifelse(group_meet$subsection == "Trade unions and professional associations" | 
                                  group_meet$subsection == "Trade and business associations" | 
                                  group_meet$subsection == "Law firms", "Other", group_meet$subsection)

pan_c <- ggplot(na.omit(group_meet), aes(x = reorder(subsection, all_meet), y = all_meet))+
  geom_bar(stat = "identity")+
  coord_flip() +
  theme_classic() +
  labs(x = NULL,
       y = "Total Number of Meetings\nBetween 2015 and 2018",
       title = "C: Total Meetings by Group Type")

pan_c


plot_grid(pan_a, pan_b, pan_c)

# number of revolvers by group type
group_rev <- anon_df %>%
  group_by(org_type) %>%
  summarise(n_rev = n()) %>%
  drop_na(org_type)

group_rev$org_type <- ifelse(group_rev$org_type == "Other public or mixed entities, created by law whose purpose is to act in the public interest", "Public sector organizations",
                             ifelse(group_rev$org_type == "Non-governmental organisations, platforms and networks and similar", "Non-governmental organizations", 
                                    ifelse(group_rev$org_type == "Professional consultancies", "Lobbying firms and consultancies", 
                                           ifelse(group_rev$org_type == "Companies & groups", "Companies", group_rev$org_type))))

ggplot(group_rev, aes(x = reorder(org_type, n_rev), y = n_rev)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  theme_bw() + 
  labs(y = "Number of Revolvers", x = NULL)

#------------------------------------------------------
# diff-in-diff results, table 3

# binary
p_meet_firm <- felm(meet ~ treat1 | 
                      career_org+ factor(career_org)*factor(year) + year_month| 
                      0 |
                      career_org,
                    data = filter(rev_df, EU_inst == 1 ))
summary(p_meet_firm)



#binary + spending controls
p_meet_spend_mod <- felm(meet ~ treat1 | 
                           career_org+factor(career_org)*factor(year) + year_month
                         + factor(year_month):log(new_cost)| 
                           0 |
                           career_org,
                         data = filter(rev_df, EU_inst == 1 ))
summary(p_meet_spend_mod )


# count
n_meet_firm <- felm(log(n_meet+.5) ~ treat1 | 
                      career_org+factor(career_org)*factor(year) + year_month| 
                      0 |
                      career_org,
                    data = filter(rev_df, EU_inst == 1 ))
summary(n_meet_firm)


# count + spending controls

n_spend_mod <- felm(log(n_meet+.5) ~ treat1 | 
                      career_org+factor(career_org)*factor(year) + year_month
                    + factor(year_month):log(new_cost)| 
                      0 |
                      career_org,
                    data = filter(rev_df, EU_inst == 1))
summary(n_spend_mod)


#----
#table

stargazer(p_meet_firm,  p_meet_spend_mod, n_meet_firm, n_spend_mod,
          keep.stat = c("n"), 
          covariate.labels = "Hire EU Official",
          dep.var.labels = c("Meeting?", "ln # Meetings + .5","Meeting?", "ln # Meetings + .5"), df = FALSE,
          type = "text", 
          title = "Hiring Former Commission Employees and Commission Meetings",
          add.lines = list(c("Organized Interest FE?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Year-Month FE?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Organized Interest X Year FE?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Spending X Year-Month FE?", "No", "Yes", "No", "Yes")))


#-----------------------------------------
# table 4: diff-in-diff, meeting content results

# binary

#abstract
abst_bin_mod <- felm(bin_abst ~treat1 | 
                       factor(career_org)*factor(year) + year_month| 
                       0 |
                       career_org,
                     data = filter(rev_df, EU_inst == 1 ))
summary(abst_bin_mod)

#specific

spec_bin_mod <- felm(bin_spec ~ treat1 | 
                       factor(career_org)*factor(year) + year_month| 
                       0 |
                       career_org,
                     data = filter(rev_df, EU_inst == 1))
summary(spec_bin_mod)

#medium
med_bin_mod <- felm(bin_med ~ treat1 | 
                      factor(career_org)*factor(year) + year_month| 
                      0 |
                      career_org,
                    data = filter(rev_df, EU_inst == 1 ))
summary(med_bin_mod)



stargazer(abst_bin_mod, med_bin_mod, spec_bin_mod,
          keep.stat = c("n"), 
          covariate.labels = "Hire EU Politician",
          dep.var.labels = c("Agenda-Setting", "Sector-Related", "Specific Policy"), df = FALSE,
          type = "text",
          title = "Hiring Former EU Official or Politician and Meeting Content",
          add.lines = list(c("Organized Interest FE?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Year-Month FE?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Organized Interest X Year FE?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Spending X Year-Month FE?", "No", "Yes", "No", "Yes")))

#-------------------------------------------------------------------------------
# Figure 3: Group type interactions

rev_df$hired_gun <- ifelse(rev_df$new_sec == "Professional consultancies", 1,0)
rev_df$ngo <- ifelse(rev_df$new_sec == "Non-governmental organisations, platforms and networks and similar", 1,0)
rev_df$firm <- ifelse(rev_df$new_sec == "Companies & groups", 1,0)
rev_df$tank <- ifelse(rev_df$new_sec == "Think tanks and research institutions", 1,0)


rev_df <- rev_df %>%
  mutate(all_type = case_when(subsection == "Companies & groups" ~ 3,
                              subsection == "Non-governmental organisations, platforms and networks and similar" ~ 2,
                              subsection == "Professional consultancies" ~ 1,
                              subsection == "Think tanks and research institutions" ~ 4,
                              TRUE ~ 5))

rev_df$all_type <- ifelse(rev_df$hired_gun == 1, 1,
                         ifelse(rev_df$ngo == 1, 2,
                                ifelse(rev_df$firm == 1, 3, 
                                       ifelse(rev_df$new_sec == "Think tanks and research institutions", 4, 5))))

#contract lobbyists
lobby_mod <- felm(meet ~ treat1*relevel(factor(all_type),ref="1") | 
                    career_org+factor(career_org)*factor(year) + year_month| 
                    0 |
                    career_org,
                  data = filter(rev_df, EU_inst == 1))
summary(lobby_mod)

n_lobby_mod <- felm(log(n_meet+.5) ~ treat1*relevel(factor(all_type),ref="1") | 
                      career_org+factor(career_org)*factor(year) + year_month| 
                      0 |
                      career_org,
                    data = filter(rev_df, EU_inst == 1))
summary(n_lobby_mod)

#NGOs
ngo_mod <- felm(meet ~ treat1*relevel(factor(all_type),ref="2") | 
                  career_org+factor(career_org)*factor(year) + year_month| 
                  0 |
                  career_org,
                data = filter(rev_df, EU_inst == 1 ))
summary(ngo_mod)

n_ngo_mod <- felm(log(n_meet+.5) ~ treat1*relevel(factor(all_type),ref="2") | 
                    career_org+factor(career_org)*factor(year) + year_month| 
                    0 |
                    career_org,
                  data = filter(rev_df, EU_inst == 1))
summary(n_ngo_mod)

#firms

firm_mod <- felm(meet ~ treat1*relevel(factor(all_type),ref="3") | 
                   career_org+factor(career_org)*factor(year) + year_month| 
                   0 |
                   career_org,
                 data = filter(rev_df, EU_inst == 1))
summary(firm_mod)

n_firm_mod <- felm(log(n_meet+.5) ~ treat1*relevel(factor(all_type),ref="3") | 
                     career_org+factor(career_org)*factor(year) + year_month| 
                     0 |
                     career_org,
                   data = filter(rev_df, EU_inst == 1))
summary(n_firm_mod)


# put results together

type_res <- rbind.data.frame(data.frame(pe = coef(lobby_mod)[1],
                                        se = sqrt(diag(vcov(lobby_mod)))[1],
                                        type = "Lobbying Firms & Consultancies"),
                             data.frame(pe = coef(n_ngo_mod)[1],
                                        se = sqrt(diag(vcov(ngo_mod)))[1],
                                        type = "NGOs"),
                             data.frame(pe = coef(firm_mod)[1],
                                        se = sqrt(diag(vcov(firm_mod)))[1],
                                        type = "Corporations"))


ggplot(type_res, aes(x = pe, y = type)) +
  geom_point() +
  geom_errorbarh(aes(xmin = pe-1.96*se,
                     xmax = pe+1.96*se), height = 0, colour = "grey") +
  geom_errorbarh(aes(xmin = pe-1.645*se,
                     xmax = pe+1.645*se), height = 0) +
  theme_classic() +
  geom_vline(xintercept = 0, lty = 3) +
  labs(x = "Differences-in-Differences\nBy Group Type",
       y = NULL)


